var LCA = {
	"id" : "165895456757483",
	"name" : "Lambda Chi Alpha Fraternity",
	"picture" : "http://profile.ak.fbcdn.net/static-ak/rsrc.php/v1/yK/r/rBaa3aPArM6.png",
	"link" : "http://www.facebook.com/pages/Lambda-Chi-Alpha-Fraternity/165895456757483",
	"category" : "Local business",
	"is_community_page" : true,
	"location" : {
		"street" : "30 Dean Street",
		"city" : "Worcester",
		"state" : "MA",
		"country" : "United States",
		"zip" : "01609",
		"latitude" : 42.272579299127,
		"longitude" : -71.804413469757
	}
};

reloadWrap = function() {
	window.location.reload();
};

$("body").ready(function() {

	// User banner
	$("#userInfo").hide();
	$("#userClick").click(function() {
		if ($('#userInfo').css('display') == 'none') {
			$("#userInfo").slideDown(400, function() {
			});
		} else
			$("#userInfo").slideUp(400);
	});
	
	// Event Location
	$("#fbPlaceResults").change(function(selectEvent) {
		console.log($("#fbPlaceResults").children(":selected")[0].innerHTML);
		$("#eventWhere")[0].value = $("#fbPlaceResults").children(":selected")[0].innerHTML;
	}); // fbPlaceResults.change

});

/**
 * Load and initialize the Facebook SDK.
 * 
 * Provides FB.api('/some-user', function(user) { }); User 'me' acesses the
 * current user
 */
window.fbAsyncInit = function() {
	FB.init({
		appId : '257615834280234',
		status : true,
		cookie : true,
		xfbml : true
	});
	
	// Unkide the FB icon in the user bar if logged in
	FB.getLoginStatus(function(response) {
		if (response.status == 'connected') {
			$("#fbConnected").css('display', 'inline');
		}
	});

	// Reload on login event to pass cookie to server
	FB.Event.subscribe('auth.login', reloadWrap);
	
	// Graceful Login
	window.fbLogin = function(callback) {
		FB.getLoginStatus(function(response) {
			if (response.status != 'connected') {
				FB.login(callback);
			} else {
				if(typeof(callback) == 'function')
					callback();
			}
		});
	};

	// Graceful Logout
	window.logout = function() {
		FB.getLoginStatus(function(response) {
			if (response.status == 'connected') {
				FB.logout(function(r) {
					window.location.href = '/logout';
				});
			} else {
				window.location.href = '/logout';
			}
		});
	};

	// Event Checkin
	window.checkin = function(place, message) {
		FB.Event.unsubscribe('auth.login', reloadWrap);
		var myLogin = function() {
			FB.login(function(loginR) {
				console.log('Logged in. UID: ' + loginR.session.uid);
				console.log('Place ID: ' + place);
				// var placeId = '165895456757483'; // 30 Dean St. 'place' ID
				var uid = loginR.session.uid;

				FB.api('/' + place, function(placeR) {
					FB.api('/' + uid + '/checkins', 'POST', {
						place : placeR.id,
						message : message,
						coordinates : placeR.location
					}, function(postR) {
						var str = '';
						for ( var l in postR.error) {
							str += l + ': ' + postR.error[l] + '\n';
						}
						console.log('Check In\n' + str);
						window.location.reload();
					});
				});
			}, {
				scope : 'email,publish_checkins'
			});
		};

		FB.getLoginStatus(function(response) {
//			console.log('FB Connection Status: ' + response.status);
			if (response.status == 'connected') {
				FB.logout(function(logoutR) {
					myLogin();
				});
			} else {
				myLogin();
			}
		});
	};

	$("body").ready(function() {
		
		// Event Location
		var timer = 0;
		$("#fbPlaceSearch").keypress(function(keyEvent) {
			clearTimeout(timer);
			timer = setTimeout(function() {
				FB.api('/search', {
					q : $("#fbPlaceSearch")[0].value,
					type : 'place',
					center : LCA.location.latitude + ','
							+ LCA.location.longitude,
					distance : 1000
				}, function(response) {
					var ln = response.data.length > 16 ? 15
							: response.data.length;
					$("#fbPlaceResults").children().remove();
					for ( var i = 0; i < ln; i++) {
						$("#fbPlaceResults").append(
								'<option value=\''
										+ response.data[i].id
										+ '\'>'
										+ response.data[i].name
										+ '</option>');
					}
				});
			}, 500);
		}); // fbPlaceSearch
		
	}); // body.ready()
};

// Asynchronously fetch the Facebook SDK
(function(d) {
	var js, id = 'facebook-jssdk';
	if (d.getElementById(id)) {
		return;
	}
	js = d.createElement('script');
	js.id = id;
	js.async = true;
	js.src = "//connect.facebook.net/en_US/all.js";
	d.getElementsByTagName('head')[0].appendChild(js);
}(document));
